//
//  OrderTimeViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/4/10.
//  Copyright (c) 2017 蓝布鲁. All rights reserved.
//

#import "OrderTimeViewController.h"
#import "UserInfoModel.h"
#import "ScheduleModel.h"
#import "MySlideBar.h"
#import "OrderTimeView.h"
#import "OrderTypeModel.h"

@interface OrderTimeViewController ()
// 数据
@property (nonatomic, retain)NSMutableArray *scheduleModelArray;            // 数据数组
@property (nonatomic,retain) NSMutableArray *orderModelArray;

// 图像
@property (nonatomic, retain)MySlideBar *slideBar;
@property (nonatomic, retain)NSMutableArray *timeViewArray;

@end

@implementation OrderTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择预约时间";
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = false;

    [self setupView];
    [self updateOrderTime];
}

#pragma mark - 自定义方法
-(void)setupView {
    // 设置顶部sliderBar
    self.slideBar = [[MySlideBar alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];



    self.slideBar.itemColor = [UIColor blackColor];
    self.slideBar.itemSelectedColor = [UIColor orangeColor];
    self.slideBar.sliderColor = [UIColor orangeColor];
    self.slideBar.itemScrollView.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview:self.slideBar];
}

#pragma mark - Private



-(void)updateOrderTime {
    self.scheduleModelArray = [[NSMutableArray alloc] init];
    self.timeViewArray = [[NSMutableArray alloc] init];
    self.orderModelArray = [[NSMutableArray alloc] init];
    
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/reserTeacher"
                                        parameters:@{@"tea_id": self.teacherID, @"s_id": [UserInfoModel shareInstance].stu_id}
                                           success:^(NSDictionary *respondDictionary) {
                                               // 转化为模型储存
                                               for (NSDictionary *dictionary in respondDictionary[@"schule"]) {
                                                   ScheduleModel *model = [[ScheduleModel alloc] initWithDictionary:dictionary];
                                                   [self.scheduleModelArray addObject:model];
                                               }
                                               
                                               for (NSDictionary *dictionary in respondDictionary[@"types"]) {
                                                   OrderTypeModel *model = [[OrderTypeModel alloc] initWithDictionary:dictionary];
                                                   [self.orderModelArray addObject:model];
                                               }

                                               // 检查是否模型数组为空
                                               if (self.scheduleModelArray.count == 0){
                                                   // 模型数组为空,则提示,并返回
                                                   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                                                                  message:@"该导师不可预约,请选择其他导师"
                                                                                                           preferredStyle:UIAlertControllerStyleAlert];
                                                   
                                                   UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                                                       [self.navigationController popViewControllerAnimated:true];
                                                   }];
                                                   [alert addAction:done];

                                                   [self presentViewController:alert animated:true completion:nil];
                                                   return;
                                               }


                                               NSMutableArray *dayArray = [[NSMutableArray alloc] init];
                                               for (ScheduleModel *scheduleModel in self.scheduleModelArray) {
                                                   // 取出所有日期
                                                   [dayArray addObject:[scheduleModel.date formatDate]];

                                                   // 创建view
                                                   OrderTimeView *view = [[OrderTimeView alloc] init];
                                                   view.times = scheduleModel.times;
                                                   view.types = self.orderModelArray;
                                                   view.teacherID = self.teacherID;
                                                   [self.timeViewArray addObject:view];
                                               }
                                               


                                               // 刷新sliderBar
                                               [self.slideBar setItemsTitle:dayArray pageViews:self.timeViewArray autoMargin:true];


                                           }
                                           failure:^(NSError *error) {
                                               
                                               [SVProgressHUD showfailed];
                                           }];
}

@end
